{
    "cells": [
        {
            "cell_type": "markdown",
            "id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05",
            "metadata": {},
            "source": [
                "# Simple Index Demo"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "50d3b817-b70e-4667-be4f-d3a0fe4bd119",
            "metadata": {},
            "source": [
                "#### Load documents, build the GPTSimpleVectorIndex"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "id": "690a6918-7c75-4f95-9ccc-d2c4a1fe00d7",
            "metadata": {},
            "outputs": [],
            "source": [
                "import logging\n",
                "import sys\n",
                "\n",
                "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
                "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
                "\n",
                "from gpt_index import GPTSimpleVectorIndex, SimpleDirectoryReader\n",
                "from IPython.display import Markdown, display"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "03d1691e-544b-454f-825b-5ee12f7faa8a",
            "metadata": {},
            "outputs": [],
            "source": [
                "# load documents\n",
                "documents = SimpleDirectoryReader('../paul_graham_essay/data').load_data()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "id": "ad144ee7-96da-4dd6-be00-fd6cf0c78e58",
            "metadata": {
                "scrolled": true
            },
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "INFO:root:> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
                        "> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
                        "> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
                        "INFO:root:> [build_index_from_documents] Total embedding token usage: 18509 tokens\n",
                        "> [build_index_from_documents] Total embedding token usage: 18509 tokens\n",
                        "> [build_index_from_documents] Total embedding token usage: 18509 tokens\n"
                    ]
                }
            ],
            "source": [
                "index = GPTSimpleVectorIndex.from_documents(documents, chunk_size_limit=1024)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "b6caf93b-6345-4c65-a346-a95b0f1746c4",
            "metadata": {},
            "source": [
                "#### Query Index"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "85466fdf-93f3-4cb1-a5f9-0056a8245a6f",
            "metadata": {
                "scrolled": true
            },
            "outputs": [],
            "source": [
                "# set Logging to DEBUG for more detailed outputs\n",
                "response_stream = index.query(\n",
                "    \"What did the author do growing up?\", \n",
                "    streaming=True,\n",
                "    similarity_top_k=1\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "16c15a25-15ed-4aed-813a-5c4c9182d7eb",
            "metadata": {},
            "outputs": [],
            "source": [
                "response_stream.print_response_stream()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "bdda1b2c-ae46-47cf-91d7-3153e8d0473b",
            "metadata": {},
            "outputs": [],
            "source": [
                "# can also get a normal response object\n",
                "response = response_stream.get_response()"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "gpt_retrieve_venv",
            "language": "python",
            "name": "gpt_retrieve_venv"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.9.16"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}